import java.util.List;
import java.util.Random;
import java.util.Scanner;

class RandomPicker {
    private ClassManager classManager;
    private GroupManager groupManager;
    private StudentManager studentManager;
    private Random random;

    public RandomPicker(ClassManager classManager, GroupManager groupManager, StudentManager studentManager) {
        this.classManager = classManager;
        this.groupManager = groupManager;
        this.studentManager = studentManager;
        this.random = new Random();
    }

    public void randomPick(Scanner scanner) {
        System.out.println("随机点名");
        System.out.println("1. 随机抽取小组");
        System.out.println("2. 随机在小组中抽取学生");
        System.out.println("3. 随机在班级里抽取学生");
        System.out.println("4. 给小组评分");
        System.out.println("5. 记录学生考勤");
        System.out.println("6. 导出成绩");
        System.out.print("请选择操作: ");
        int choice = scanner.nextInt();
        scanner.nextLine(); // 清除换行符

        switch (choice) {
            case 1:
                randomPickGroup();
                break;
            case 2:
                randomPickStudentInGroup(scanner);
                break;
            case 3:
                randomPickStudentInClass(scanner);
                break;
            case 4:
                rateGroup(scanner);
                break;
            case 5:
                recordAttendance(scanner);
                break;
            case 6:
                exportGrades();
                break;
            default:
                System.out.println("无效选择");
        }
    }

    private void randomPickGroup() {
        List<String> groups = groupManager.getGroups();
        if (groups.isEmpty()) {
            System.out.println("没有可用的小组");
            return;
        }
        int randomIndex = random.nextInt(groups.size());
        System.out.println("随机抽取的小组是: " + groups.get(randomIndex));
    }

    private void randomPickStudentInGroup(Scanner scanner) {
        List<String> groups = groupManager.getGroups();
        if (groups.isEmpty()) {
            System.out.println("没有可用的小组");
            return;
        }

        System.out.println("可用的小组:");
        for (int i = 0; i < groups.size(); i++) {
            System.out.println((i + 1) + ". " + groups.get(i));
        }
        System.out.print("请选择小组 (输入编号): ");
        int groupIndex = scanner.nextInt() - 1;
        scanner.nextLine(); // 清除换行符

        if (groupIndex < 0 || groupIndex >= groups.size()) {
            System.out.println("无效的小组编号");
            return;
        }

        List<String> students = studentManager.getStudents();
        if (students.isEmpty()) {
            System.out.println("没有可用的学生");
            return;
        }
        int randomIndex = random.nextInt(students.size());
        System.out.println("随机抽取的学生是: " + students.get(randomIndex));
    }

    private void randomPickStudentInClass(Scanner scanner) {
        List<String> classes = classManager.getClasses();
        if (classes.isEmpty()) {
            System.out.println("没有可用的班级");
            return;
        }

        System.out.println("可用的班级:");
        for (int i = 0; i < classes.size(); i++) {
            System.out.println((i + 1) + ". " + classes.get(i));
        }
        System.out.print("请选择班级 (输入编号): ");
        int classIndex = scanner.nextInt() - 1;
        scanner.nextLine(); // 清除换行符

        if (classIndex < 0 || classIndex >= classes.size()) {
            System.out.println("无效的班级编号");
            return;
        }

        List<String> students = studentManager.getStudents();
        if (students.isEmpty()) {
            System.out.println("没有可用的学生");
            return;
        }
        int randomIndex = random.nextInt(students.size());
        System.out.println("随机抽取的学生是: " + students.get(randomIndex));
    }

    private void rateGroup(Scanner scanner) {
        System.out.print("输入小组名称: ");
        String groupName = scanner.nextLine();
        System.out.print("输入评分 (0-100): ");
        int score = scanner.nextInt();
        scanner.nextLine(); // 清除换行符
        System.out.println("小组 " + groupName + " 的评分为: " + score);
    }

    private void recordAttendance(Scanner scanner) {
        System.out.print("输入学生姓名: ");
        String studentName = scanner.nextLine();
        System.out.print("学生 " + studentName + " 是否到场 (是/否): ");
        String attendance = scanner.nextLine();
        System.out.println("记录: " + studentName + " " + (attendance.equals("是") ? "到场" : "缺席"));
    }

    private void exportGrades() {
        // 这里可以实现导出成绩的逻辑
        System.out.println("成绩导出功能尚未实现");
    }
}